<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import itemTuwen from "./itemTuwen/index.vue";

export default {
    components: {
        itemTuwen,
    },
    props: {},
    computed: {},
    data() {
        return {
            // 外部传入
            apiConfig:null,
            // 外部传入
            appWindow: null,
            loading: true,
            // 窗口显示
            boxVisible: false,
            type: "add",

            albumId: "",
            // 图文名称
            name: "",
            // 是否同步微信 0否 1是
            isWechat: 0,
            albumInfoItemLst: [],

            id: "",

            listAlbum: [],

            selectIndex: 0,
            mode: {
                // 微信封面图片URL
                thumbUrl: "",
                // 本地封面图片URL
                localThumbUrl: "",
                // 封面图片素材id
                thumbMediaId: "",
                // 是否显示封面 0否 1是
                showCoverPic: 1,

                // 微信内容
                content: "",
                // 本地内容
                localContent: "",

                // 多媒体素材对应关系
                contentMatchLst: [],

                // 标题
                title: "",
                // 作者
                author: "",
                // 摘要
                digest: "",
                // 原文地址
                contentSourceUrl: "",
            },
        };
    },
    methods: {
        addItem() {
            this.albumInfoItemLst.push({
                ...this.mode,
            });
        },
        // 关闭窗口
        editBoxClose() {
            this.boxVisible = false;
        },
        // 取消
        editBoxCancel() {
            this.editBoxClose();
        },

        // 图片上传
        async submit() {
            if (this.albumInfoItemLst.length <= 0) {
                this.$vFN.Tools.messageError("图文内容 不能为空");
                this.addItem();
                return;
            }
            if (this.name == "") {
                this.$vFN.Tools.messageError("图文名称 不能为空");
                return;
            }
            let _is = "";
            let _index = 0;
            this.albumInfoItemLst.forEach((item) => {
                if (!_is) {
                    if (!item.thumbMediaId) {
                        _is = "封面图片 不能为空";
                    } else if (!item.title) {
                        _is = "标题 不能为空";
                    } else if (!item.localContent) {
                        _is = "内容 不能为空";
                    }
                }
            });
            if (_is) {
                this.$vFN.Tools.messageError(_is);
                this.selectIndex = _index;
                return;
            }
            let _d = {
                data: {
                    type: 4,
                    albumId: this.albumId,
                    // 图文名称
                    name: this.name,
                    // 是否同步微信 0否 1是
                    isWechat: this.isWechat,
                    albumInfoItemLst: this.albumInfoItemLst,
                },
            };

            if (this.item) {
                _d.url = this.apiConfig.updateSubmit.url;
                _d.domain = this.apiConfig.updateSubmit.domain;
                _d.data.id = this.item.id;
            } else {
                _d.url = this.apiConfig.createSubmit.url;
                _d.domain = this.apiConfig.createSubmit.domain;
            }
            console.error("_d", _d);

            let _data = await this.$vFN.Http.post(_d, this.appWindow);
            if (_data._ok) {
                this.editBoxClose();
                this.onSuccess();
                this.$vFN.Tools.messageSuccess("保存成功");
            }
        },

        async getAlbum() {
           
            let _data = await this.$vFN.Http.post(
                {
                    url: this.apiConfig.albumList.url,
                    domain: this.apiConfig.albumList.domain,
                    data: {
                        type: 4,
                    },
                },
                this.appWindow
            );
            if (_data._ok) {
                this.listAlbum = _data.data;
            }
        },
        async getData() {
            let _data = await this.$vFN.Http.post(
                {
                    url: this.apiConfig.dataInfo.url,
                    domain: this.apiConfig.dataInfo.domain,
                    data: {
                        id: this.item.id,
                    },
                },
                this.appWindow
            );
            if (_data._ok) {
                this.name = _data.data.name;
                this.isWechat = _data.data.isWechat;
                this.albumInfoItemLst = _data.data.albumInfoItemLst;
            }
        },

        async init() {
            this.loading = true;
            this.boxVisible = true;
            if (this.item) {
                await this.getData();
            } else {
                await this.addItem();
            }
            await this.getAlbum();
            this.loading = false;
        },

        // 成功回调
        onSuccess() {},
        // 取消回调
        onCancel() {},
        // 失败回调
        onError() {},
    },
    watch: {},
    mounted() {
        this.init();
    },
    beforeCreate() {},
};
</script>